package com.glsc.ngateway.platform.service.system;

import cn.hutool.core.collection.CollectionUtil;
import com.glsc.ngateway.common.api.common.utils.PageUtil;
import com.glsc.ngateway.common.base.domain.mysql.gateway.system.SystemMain;
import com.glsc.ngateway.common.base.domain.mysql.gateway.system.SystemMember;
import com.glsc.ngateway.common.base.exception.PlatformException;
import com.glsc.ngateway.common.base.repo.mysql.gateway.platform.it.ItApplicationDetailRepo;
import com.glsc.ngateway.common.base.repo.mysql.gateway.platform.it.ItOperationRepo;
import com.glsc.ngateway.common.base.repo.mysql.gateway.platform.it.ItVirtualDeviceRepo;
import com.glsc.ngateway.common.base.repo.mysql.gateway.platform.system.SystemMemberRepository;
import com.glsc.ngateway.platform.service.MailService;
import com.glsc.ngateway.platform.service.UserService;
import com.glsc.ngateway.platform.service.it.SequenceService;
import com.glsc.ngateway.platform.service.it.ServerService;
import com.glsc.ngateway.common.api.common.enums.DictConstant;
import com.glsc.ngateway.platform.utils.SpecificationUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
public class SystemMemberService {

    Logger logger = LoggerFactory.getLogger(SystemMemberService.class);
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private SequenceService sequenceService;
    @Autowired
    private UserService userService;
    @Autowired
    private ItVirtualDeviceRepo itVirtualDeviceRepo;
    @Autowired
    private ItOperationRepo itOperationRepo;
    @Autowired
    private ItApplicationDetailRepo itApplicationDetailRepo;

    @Autowired
    private ServerService serverService;

    @Autowired
    private MailService mailService;

    @Resource
    private SystemMemberRepository systemMemberRepository;


    @Transactional
    public void addOrUpdate(SystemMember systemMember) {
//        SystemVendorLogInfo log = new SystemVendorLogInfo();
//        if (StrUtil.isNotEmpty(systemMember.getVendorCode())){
//            //修改
//            log.setLogFlag(DictConstant.UPDATE);
//        } else {
//            //新增
//            log.setLogFlag(DictConstant.ADD);
//        }
//        BeanUtil.copyProperties(systemMember,log);
        systemMember.setDeleted(DictConstant.NO_DELETE);
        systemMemberRepository.save(systemMember);
//        systemVendorLogInfoRepository.save(log);
    }

    public void delete(Integer id) {
        Optional<SystemMember> optional = systemMemberRepository.findById(id);
        if (optional.isPresent()) {
            SystemMember bean = optional.get();
            bean.setDeleted(DictConstant.DELETED);
            systemMemberRepository.save(bean);
        } else {
            throw PlatformException.errorWithOutMail("删除记录不存在");
        }
    }

    public List<SystemMember> search(SystemMember params) {
        params.setDeleted(DictConstant.NO_DELETE);
        List<SystemMember> result = systemMemberRepository.searchByParam(params);
        return result;
    }

    @Cacheable(cacheNames = "querySystemCodeByMemberAccount#6000")
    public List<String> querySystemCodeByMemberAccount(List<String> account) {
        List<SystemMain> systemMains = systemMemberRepository.querySystemCodeByMemberAccount(account);
        if (CollectionUtil.isNotEmpty(systemMains)) {
            return systemMains.stream().map(SystemMain::getSystemClassNo).collect(Collectors.toList());
        } else {
            //不返回null
            return new ArrayList<>();
        }
    }


    public Page<SystemMember> page(int pageNo, int pageSize, Map<String, Object> param) {
        Sort.Direction direction = Sort.Direction.DESC;
        String sort = "id";

        pageNo = PageUtil.initPageNo(pageNo);
        pageSize = PageUtil.initPageSize(pageSize);

        Pageable pageable = PageRequest.of(pageNo - 1, pageSize, direction, sort);
        param.put("EQ_deleted", 0);
        Page<SystemMember> page = systemMemberRepository.findAll(SpecificationUtil.buildSpecification(param, SystemMember.class), pageable);

        return page;
    }
}
